package com.tlw.practice.tlw.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.tlw.practice.tlw.dao.LeaveDao;
import com.tlw.practice.tlw.entity.AuditEntity;
import com.tlw.practice.tlw.entity.LeaveEntity;
import com.tlw.practice.tlw.pojo.form.RequestLeaveForm;
import com.tlw.practice.tlw.pojo.form.LeaveForm;
import com.tlw.practice.tlw.service.AuditService;
import com.tlw.practice.tlw.service.LeaveService;
import com.tlw.practice.tlw.utils.BeanChangeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.transaction.Transactional;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @program: tlw
 * @description:
 * @author: Genmer
 * @create: 2020-12-03 00:38
 **/
@Service
public class LeaveServiceImpl implements LeaveService {

    @Autowired
    private LeaveDao leaveDao;

    @Autowired
    private AuditService auditService;

    // 时间格式化
    SimpleDateFormat fm=new SimpleDateFormat("yyyyMMdd HH:mm:ss");
    SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");


    /**
     * 新增请假单
     * @param requestLeave
     * @return
     */
    @Override
    @Transactional
    public int addRequestLeave(LeaveEntity requestLeave) {
        LeaveEntity save = leaveDao.save(requestLeave);
        return (save != null) ? 1 : 0;
    }


    /**
     * 删除请假单
     * @param requestId
     * @return
     */
    @Override
    @Transactional
    public int deleteRequestLeave(String requestId) {
        leaveDao.deleteById(requestId);
        return 1;
    }

    /**
     * 更新请假单
     * @param requestLeave
     * @return
     */
    @Override
    @Transactional
    public int updateRequestLeave(LeaveForm requestLeave, String id) throws InterruptedException {
        // dto转换实体类
        LeaveEntity leaveEntity = new LeaveEntity();
        leaveEntity.setId(id);
        BeanUtil.copyProperties(requestLeave, leaveEntity);
        // 调用工具类记录变更
        BeanChangeUtil util = new BeanChangeUtil();
        AuditEntity result = util.recordChange(leaveEntity);
        result.setOperationType("更新");
        auditService.saveAudit(result);
        return leaveDao.save(leaveEntity)!= null ? 1 : 0;
    }

    /**
     * 获取单个请假单
     * @param requestId
     * @return
     */
    @Override
    public LeaveEntity findOneRequestLeaveById(String requestId) {
        return Optional.ofNullable(leaveDao.findOneById(requestId)).orElse(null);
    }

    /**
     * 无条件分页查询
     * @param pageable
     * @return
     */
    @Override
    public Page<LeaveEntity> findRequestLeaveNoQuery(Pageable pageable) {
        return Optional.ofNullable(leaveDao.findAll(pageable)).orElse(null);
    }

    /**
     * 带条件分页查询
     * @param pageable
     * @param query
     * @return
     */
    @Override
    public Page<LeaveEntity> findRequestLeaveQuery(Pageable pageable, String query) throws ParseException {
        return leaveDao.findAll(new Specification<LeaveEntity>() {
            @Override
            public Predicate toPredicate(Root<LeaveEntity> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {
                List<Predicate> predicates = new ArrayList<>();
//                System.err.println(root.get("leaveType"));
                if (query != null) {
                    predicates.add(cb.equal(root.<String>get("leaveType"),  query));
                }
//                if (query.getLeaveTime() != null) {
//                    predicates.add(cb.between(root.<Date>get("leaveTime"), query.getLeaveTime().getLeaveTimeFrom(), query.getLeaveTime().getLeaveTimeTo()));
//                }
                cq.where(predicates.toArray(new Predicate[predicates.size()]));
                return null;
            }
        }, pageable);
    }
}
